package com.cst.intercepterdemo.chain.builder;

import com.cst.intercepterdemo.chain.expression.parser.DefaultExpressionParser;
import com.cst.intercepterdemo.chain.expression.parser.IExpressionParser;
import com.cst.intercepterdemo.exceptions.ApiNotCorrectDefineException;
import com.cst.intercepterdemo.exceptions.ExpressionNotCorrectException;
import com.cst.intercepterdemo.mapper.ApiMapper;
import com.cst.intercepterdemo.mapper.ApiValidatorMapper;
import com.cst.intercepterdemo.model.Api;
import com.cst.intercepterdemo.model.ApiExample;
import com.cst.intercepterdemo.model.ApiValidator;
import com.cst.intercepterdemo.model.ApiValidatorExample;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * <ul>
 * <li>文件名称 : com.cst.intercepterdemo.chain.builder.DBChainBuilder
 * </li>
 * <li>版权所有 : 版权所有(C)2016-2020</li>
 * <li>公 司 : CST</li>
 * <li>创建时间 : 2016/12/2</li>
 * <li>修改记录 : </li>
 * </ul>
 * 类说明：责任链建造者实现（面向数据库配置）
 *
 * @author Allen
 * @version 1.0
 */
public class ExpressionBaseDBChainBuilder extends AbstractExpressionBaseChainBuilder {

    private static final IExpressionParser DEFAULT_PARSER = new DefaultExpressionParser();

    @Autowired
    private ApiValidatorMapper apiValidatorDao;

    @Autowired
    private ApiMapper apiDao;

    @Override
    protected String getExpressionString(String key) {

        ApiExample apiExample = new ApiExample();
        apiExample.createCriteria().andApiEqualTo(key);
        List<Api> apis = apiDao.selectByExample(apiExample);
        if(apis==null || apis.size()==0){
            throw new ApiNotCorrectDefineException(key);
        }

        ApiValidatorExample example = new ApiValidatorExample();
        example.createCriteria().andApiidEqualTo(apis.get(0).getId());

        List<ApiValidator> interfaceValidators =
                apiValidatorDao.selectByExample(example);

        if(interfaceValidators==null || interfaceValidators.size()==0){
            throw new ExpressionNotCorrectException();
        }
        return interfaceValidators.get(0).getExpression();
    }

    @Override
    protected List<ExpressionData> getAllExpressionString() {
        ApiValidatorExample example = new ApiValidatorExample();
        List<ApiValidator> interfaceValidators
                = apiValidatorDao.selectByExample(example);
        List<ExpressionData> targetList = new ArrayList<>();
        for (ApiValidator interfaceValidator : interfaceValidators) {

            ApiExample apiExample = new ApiExample();
            apiExample.createCriteria().andIdEqualTo(interfaceValidator.getApiid());
            List<Api> apis = apiDao.selectByExample(apiExample);
            if(apis==null || apis.size()==0){
                throw new ApiNotCorrectDefineException("id:"+interfaceValidator.getId().toString());
            }

            targetList.add(new ExpressionData(
                    apis.get(0).getApi()
                    ,interfaceValidator.getExpression()));
        }
        return targetList;
    }

    @Override
    protected IExpressionParser getExpressionParser(String key) {
        return getDefaultParser();
    }

    private IExpressionParser getDefaultParser(){
        return DEFAULT_PARSER;
    }
}
